#include "ALSSR.h"


CALSSR::CALSSR()
{
}

CALSSR::CALSSR(CALStepItem* pStepItem)
{
	m_pStepItem = pStepItem;
}

CALSSR::~CALSSR()
{
}

void CALSSR::test()
{
	//Mat grayTemp, grayDown;
	//vector<Mat> mv;
	////Size imageSize(160,120);
	//Size imageSize((*src).cols, (*src).rows);
	//Mat realImage(imageSize, CV_64F);
	//Mat imaginaryImage(imageSize, CV_64F); imaginaryImage.setTo(0);
	//Mat combinedImage(imageSize, CV_64FC2);
	//Mat imageDFT;
	//Mat logAmplitude;
	//Mat angle(imageSize, CV_64F);
	//Mat magnitude(imageSize, CV_64F);
	//Mat logAmplitude_blur;

	//cvtColor(*src, grayTemp, CV_BGR2GRAY);
	//resize(grayTemp, grayDown, imageSize, 0, 0, INTER_LINEAR);
	//for (int j = 0; j<grayDown.rows; j++)
	//	for (int i = 0; i<grayDown.cols; i++)
	//		realImage.at<double>(j, i) = grayDown.at<uchar>(j, i);

	//mv.push_back(realImage);
	//mv.push_back(imaginaryImage);
	//merge(mv, combinedImage);
	//dft(combinedImage, imageDFT);
	//split(imageDFT, mv);

	////-- Get magnitude and phase of frequency spectrum --//
	//cartToPolar(mv.at(0), mv.at(1), magnitude, angle, false);
	//log(magnitude, logAmplitude);
	////-- Blur log amplitude with averaging filter --//
	//blur(logAmplitude, logAmplitude_blur, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);

	//exp(logAmplitude - logAmplitude_blur, magnitude);
	////-- Back to cartesian frequency domain --//
	//polarToCart(magnitude, angle, mv.at(0), mv.at(1), false);
	//merge(mv, imageDFT);
	//dft(imageDFT, combinedImage, CV_DXT_INVERSE);
	//split(combinedImage, mv);

	//cartToPolar(mv.at(0), mv.at(1), magnitude, angle, false);
	//GaussianBlur(magnitude, magnitude, Size(5, 5), 8, 0, BORDER_DEFAULT);
	//magnitude = magnitude.mul(magnitude);

	//double minVal, maxVal;
	//minMaxLoc(magnitude, &minVal, &maxVal);
	//magnitude = magnitude / maxVal;//normalize

	//Mat tempFloat(imageSize, CV_32F);

	//for (int j = 0; j<magnitude.rows; j++)
	//	for (int i = 0; i<magnitude.cols; i++)
	//		tempFloat.at<float>(j, i) = magnitude.at<double>(j, i);

	//resize(tempFloat, *dst, dst->size(), 0, 0, INTER_LINEAR);
}

void CALSSR::convert()
{
	if (m_pStepItem == NULL) return;

	try
	{
		CALSISSR* pStepItem = (CALSISSR*)m_pStepItem;

		calculateSaliencyMap(&m_srcMat, &m_dstMat);

		SetDstType(AL_DST_Mat_One);
	}
	catch (...)
	{

	}
}

void CALSSR::convert(const Mat &src, Mat &dst)
{

}


void CALSSR::calculateSaliencyMap(const Mat* src, Mat* dst)
{
	Mat grayTemp, grayDown;
	vector<Mat> mv;
	//Size imageSize(160,120);
	Size imageSize((*src).cols, (*src).rows);
	Mat realImage(imageSize, CV_64F);
	Mat imaginaryImage(imageSize, CV_64F); imaginaryImage.setTo(0);
	Mat combinedImage(imageSize, CV_64FC2);
	Mat imageDFT;
	Mat logAmplitude;
	Mat angle(imageSize, CV_64F);
	Mat magnitude(imageSize, CV_64F);
	Mat logAmplitude_blur;

	cvtColor(*src, grayTemp, CV_BGR2GRAY);
	resize(grayTemp, grayDown, imageSize, 0, 0, INTER_LINEAR);
	for (int j = 0; j<grayDown.rows; j++)
		for (int i = 0; i<grayDown.cols; i++)
			realImage.at<double>(j, i) = grayDown.at<uchar>(j, i);

	mv.push_back(realImage);
	mv.push_back(imaginaryImage);
	merge(mv, combinedImage);
	dft(combinedImage, imageDFT);
	split(imageDFT, mv);

	//-- Get magnitude and phase of frequency spectrum --//
	cartToPolar(mv.at(0), mv.at(1), magnitude, angle, false);
	log(magnitude, logAmplitude);
	//-- Blur log amplitude with averaging filter --//
	blur(logAmplitude, logAmplitude_blur, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);

	exp(logAmplitude - logAmplitude_blur, magnitude);
	//-- Back to cartesian frequency domain --//
	polarToCart(magnitude, angle, mv.at(0), mv.at(1), false);
	merge(mv, imageDFT);
	dft(imageDFT, combinedImage, CV_DXT_INVERSE);
	split(combinedImage, mv);

	cartToPolar(mv.at(0), mv.at(1), magnitude, angle, false);
	GaussianBlur(magnitude, magnitude, Size(5, 5), 8, 0, BORDER_DEFAULT);
	magnitude = magnitude.mul(magnitude);

	double minVal, maxVal;
	minMaxLoc(magnitude, &minVal, &maxVal);
	magnitude = magnitude / maxVal;//normalize

	Mat tempFloat(imageSize, CV_32F);

	for (int j = 0; j<magnitude.rows; j++)
		for (int i = 0; i<magnitude.cols; i++)
			tempFloat.at<float>(j, i) = magnitude.at<double>(j, i);

	resize(tempFloat, *dst, dst->size(), 0, 0, INTER_LINEAR);
}

